import { currentInstance } from "./component.js";

export function provide(key, value) {
  if (!currentInstance) return;
  const parentProvides =
    currentInstance.parent && currentInstance.parent.provides;
  let provides = currentInstance.provides;

  // 如果当前实例没有provides，则创建一个空对象，并将父组件的provides赋值给当前实例的provides
  if (!provides) {
    provides = currentInstance.provides = Object.create(parentProvides);
  }
  provides[key] = value;
}

export function inject(key, defaultVal) {
  if (!currentInstance) return;
  const provides = currentInstance.parent.provides;
  if (provides && key in provides) {
    return provides[key];
  } else {
    return defaultVal; // 如果没有找到，返回默认值
  }
}
